var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: -1,
        aritcle: {},
        comment: {
            count: 0,
            data: []
        },

        // 浮动按钮
        menuBtn: [{
            type: "homepage",
            title: "首页",
            span: 6
        },
        {
            type: "collection",
            span: 6,
            title: "收藏"
        },
        {
            type: "message",
            span: 6,
            title: "评论"
        },
        {
            type: "share",
            title: "分享",
            span: 6,
            openType: "share"
        }
        ],

        // 是否显示评论
        commentVisible: false,
        // 评论类型 0评论 1回复
        commentType: 0,
        // 评论id
        commentId: 0,
        // 评论名称
        commentName: '',
        // 目标id
        targetUserId: 0,
        // 评论内容
        content: '',
        // 分页
        page: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id
        });
        this.getAritcleDetail();
        this.getCommentList();
        this.addArticleLookNum();
        this.chuckArticleCollect();
    },

    onPullDownRefresh: function () {
        this.setData({
            aritcle: {},
            comment: {
                count: 0,
                data: [],
            },
            // 是否显示评论
            commentVisible: false,
            // 评论类型 0评论 1回复
            commentType: 0,
            // 评论id
            commentId: 0,
            // 评论名称
            commentName: '',
            // 目标id
            targetUserId: 0,
            // 评论内容
            content: '',
            // 分页
            page: 1
        });
        this.getAritcleDetail();
        this.getCommentList();
        wx.stopPullDownRefresh();
    },

    onReachBottom() {
        this.getCommentList();
    },

    // 获取文章详情
    getAritcleDetail() {
        common.post(url.getArticleDetail, { id: this.data.id }, (data) => {
            if (data.images == null)
                data.images = [];
            else {
                data.images = (data.images + '').split(',');
                data.images = data.images.map((n) => {
                    return n;
                })
            }
            this.setData({ aritcle: data });
        });
    },

    // 获取评论
    getCommentList() {
        let page = this.data.page;
        if (page === false) return;
        this.setData({
            page: page + 1
        })
        common.post(url.getArticleCommentList, { article_id: this.data.id, page: page }, (data) => {
            let comment = this.data.comment;
            comment.data = comment.data.concat(data.data);
            comment.count = data.count;
            this.setData({ comment });
            if (data.data.length == 0) {
                this.setData({
                    page: false
                })
            }
        });
    },

    // 选择浮动按钮
    onMenuClicked(e) {
        let index = e.detail.index;
        switch (index) {
            case 0:
                wx.switchTab({
                    url: '/pages/index/index'
                })
                break;
            case 1:
                this.addArticleCollect();
                break;
            case 2:
                this.setData({ commentType: 0, commentName: '', targetUserId: 0 });
                this.showComment();
                break;
            case 3:
                wx.showShareMenu({
                    withShareTicket: true
                });

                break;
        }
    },

    showComment() {
        this.setData({
            commentVisible: true,
        })
    },

    onChangeValue(e) {
        let value = e.detail.detail.value;
        this.setData({ content: value });
    },

    // 评论关闭
    onCommentClose(e) {
        this.setData({
            commentVisible: false,
        })
    },

    // 发表评论
    sendComment() {
        let content = this.data.content;
        if (content.length == 0) {
            wx.showModal({
                title: '提示',
                content: '内容不能为空',
                success(res) { }
            })
            return;
        }

        wx.showLoading({ mask: true });
        if (this.data.commentType == 0) {
            common.post(url.addArticleComment, {
                content: content,
                article_id: this.data.id,
            }, (data) => {
                wx.hideLoading();
                wx.showToast({
                    title: '评论成功',
                    icon: 'success',
                    duration: 2000
                })
                this.setData({
                    content: '',
                    commentVisible: false,
                    comment: {
                        count: 0,
                        data: [],
                    },
                    // 是否显示评论
                    commentVisible: false,
                    // 评论类型 0评论 1回复
                    commentType: 0,
                    // 评论id
                    commentId: 0,
                    // 评论名称
                    commentName: '',
                    // 目标id
                    targetUserId: 0,
                    // 评论内容
                    content: '',
                    // 分页
                    page: 1
                });
                this.getCommentList();
            });
        } else {
            common.post(url.addArticleReply, {
                content: content,
                comment_id: this.data.commentId,
                target_user_id: this.data.targetUserId,
            }, (data) => {
                wx.hideLoading();
                wx.showToast({
                    title: '评论成功',
                    icon: 'success',
                    duration: 2000
                })
                this.setData({
                    content: '',
                    commentVisible: false,
                    comment: {
                        count: 0,
                        data: [],
                    },
                    // 是否显示评论
                    commentVisible: false,
                    // 评论类型 0评论 1回复
                    commentType: 0,
                    // 评论id
                    commentId: 0,
                    // 评论名称
                    commentName: '',
                    // 目标id
                    targetUserId: 0,
                    // 评论内容
                    content: '',
                    // 分页
                    page: 1
                });
                this.getCommentList();
            });
        }
    },

    /**
     * 文章回复
     * @param {*} param0 
     */
    tapCommentReplyer({ currentTarget }) {
        let data = currentTarget.dataset;
        this.setData({ commentType: 1, commentName: data.username, targetUserId: data.userid, commentId: data.id });
        this.showComment();
    },

    /**
     * 添加文章浏览量
     */
    addArticleLookNum() {
        common.post(url.addArticleLookNum, { article_id: this.data.id }, () => { });
    },

    /**
     * 添加文章分享数
     */
    addArticleShareNum() {
        common.post(url.addArticleShareNum, { article_id: this.data.id }, () => { });
    },

    /**
     * 检测用户是否收藏
     */
    chuckArticleCollect() {
        let menuBtn = this.data.menuBtn;
        common.post(url.chuckArticleCollect, { article_id: this.data.id }, (data) => {
            if (data) {
                menuBtn[1].type = 'collection_fill';
                menuBtn[1].title = '取消收藏';
            } else {
                menuBtn[1].type = 'collection';
                menuBtn[1].title = '收藏';
            }
            this.setData({ menuBtn });
        });
    },

    onUserInfo({ currentTarget }) {
        var data = currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/user_info/index?id=' + data.id
        });
    },
    /**
     * 添加文章收藏
     */
    addArticleCollect() {
        let menuBtn = this.data.menuBtn;
        let aritcle = this.data.aritcle;
        wx.showLoading({ mask: true });
        common.post(url.addArticleCollect, { article_id: this.data.id }, (data) => {
            wx.hideLoading();
            if (data) {
                menuBtn[1].type = 'collection_fill';
                menuBtn[1].title = '取消收藏';
                aritcle.collect_num = aritcle.collect_num + 1;
            } else {
                menuBtn[1].type = 'collection';
                menuBtn[1].title = '收藏';
                aritcle.collect_num = aritcle.collect_num - 1;
            }
            this.setData({ menuBtn, aritcle });
        });
    },

    /**
     * 分享
     * @param {*} e 
     */
    onShareAppMessage(e) {
        wx.showShareMenu({
            withShareTicket: true
        });

        this.addArticleShareNum();
    }

})